﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Toolkit 的摘要说明
/// </summary>
public class Toolkit
{
    private static string PreviousUUID = null;

	public Toolkit()
	{
	}

    public static Setting GetSettingValue(string key)
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aS in db.Setting
                where aS.SettingKey == key
                select aS;
        if (q.Count() == 0)
        {
            return null;
        }
        else
        {
            return q.First();
        }
    }

    public static List<Setting> GetSettingValues(string key)
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aS in db.Setting
                where aS.SettingKey == key
                select aS;
        if (q.Count() == 0)
        {
            return null;
        }
        else
        {
            return q.ToList();
        }
    }

    public static int PageSize()
    {
        Setting s = GetSettingValue("PageSize");
        if (s == null)
        {
            return 10;
        }
        else
        {
            return Convert.ToInt32(s.Value);
        }
    }

    public static string NextUUID()
    {
        long time;
        string nu;

        do
        {
            time = DateTime.Now.Ticks;
            nu = Convert.ToString(time, 16);
        } while (nu == PreviousUUID);

        PreviousUUID = nu;

        return PreviousUUID;
    }

    public static bool hasPower(int userpower, Powers power)
    {
        int p = (int)power;
        return (userpower & p) == p;
    }
}